package org.hxyjs.dao.impl;

import org.hxyjs.dao.AdvertisingtypeDao;
import org.hxyjs.entity.Advertisingtype;
import org.hxyjs.entity.Advertisingtype;
import org.hxyjs.mapper.AdvertisingtypeMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("advertisingtypeDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class AdvertisingtypeDaoImpl implements AdvertisingtypeDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private AdvertisingtypeMapper advertisingtypeMapper;
    public List<Advertisingtype>  getAllAdvertisingtypes() throws Exception{
        return advertisingtypeMapper.selectByExample(null);
    }

    @Override
    public Integer deleteAdvertisingtypeByAdvrno(String advrno)  throws Exception{
        return advertisingtypeMapper.deleteByPrimaryKey(advrno);
    }

    @Override
    public Integer updateAdvertisingtypeByAdvrno(Advertisingtype advertisingtype)  throws Exception{
        return advertisingtypeMapper.updateByPrimaryKey(advertisingtype);
    }

    @Override
    public Integer addAdvertisingtype(Advertisingtype advertisingtype)  throws Exception{
        advertisingtype.setAdvrno(initId());
        return advertisingtypeMapper.insertSelective(advertisingtype);
    }
    @Override
    public List<Advertisingtype> getAdvertisingtypeByPage(PageInfo pageInfo )  throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount = (int)advertisingtypeMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return advertisingtypeMapper.getAdvertisingtypeByPage(map);
    }

    public String initId(){
        String maxAdvrno = advertisingtypeMapper.getMaxAdvrno();
        System.out.println(GenNumTools.initId("",3,maxAdvrno));
        return GenNumTools.initId("",3,maxAdvrno);
    }
}
